package co.fitstart.weapp.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.market.satisfaction.SatisfactionSurvey;
import co.fitstart.entity.user.Student;
import co.fitstart.weapp.api.dao.SatisfactionSurveyDao;
import co.fitstart.weapp.api.service.SatisfactionSurveyService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("satisfactionSurveyService")
public class SatisfactionSurveyServiceImpl implements SatisfactionSurveyService {

    /** 满意度礼品券批次 */
    private static final long COUPON_BATCH_ID = 16l;

    @Dao
    private SatisfactionSurveyDao satisfactionSurveyDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.SatisfactionSurveyService#load(co.fitstart.entity.user.Student)
     */
    public SatisfactionSurvey load(Student student) {
        return satisfactionSurveyDao.load(student);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.SatisfactionSurveyService#update(co.fitstart.entity.market.satisfaction.SatisfactionSurvey)
     */
    public void update(SatisfactionSurvey survey) {
        CouponBatch couponBatch = genericDaoSupport.load(CouponBatch.class, COUPON_BATCH_ID);
        genericDaoSupport.update(couponBatch.editClaimedAmount(1).editReleaseAmount(1));
        
        Coupon coupon = new Coupon(couponBatch);
        genericDaoSupport.save(coupon.generateCode().bindUser(survey.getCourseRound().getStudent().getUser().getId()));
        
        genericDaoSupport.update(survey.bindCoupon(coupon));
    }

}
